package com.shundr.auth.common.view;

import java.util.ArrayList;
import java.util.List;

import com.shundr.auth.R;
import com.shundr.auth.user.model.AttributeInfo;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class DialogChoiceLength extends Dialog {
	Context context;
	private TextView tvTitle;
	private ImageView ivCancel;
	private GridView gvGridview;
	private String title;

	private OnSelectListener mSelectListener;
	/**
	 * 筛选数据 适配器 list
	 */
	private List<AttributeInfo> mList = new ArrayList<AttributeInfo>();

	public DialogChoiceLength(Context context, String title,
			OnSelectListener selectListener) {
		super(context, R.style.dialog_style);
		this.context = context;
		this.title = title;
		this.mSelectListener = selectListener;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_truck_info_choice);
		setCanceledOnTouchOutside(true);
		tvTitle = (TextView) findViewById(R.id.tv_dialog_gridview_title);
		ivCancel = (ImageView) findViewById(R.id.iv_dialog_gridview_cancel);
		gvGridview = (GridView) findViewById(R.id.gv_dialog_gridview_id);
		try {
			tvTitle.setText(title);
			initData();
		} catch (Exception e) {
			e.printStackTrace();
		}
		SimpleAdapter adapter = new SimpleAdapter();
		gvGridview.setAdapter(adapter);
		gvGridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				try {
					AttributeInfo info = mList.get(position);
					mSelectListener.selected(info);
				} catch (Exception e) {
					e.printStackTrace();
				}
				dismiss();
			}
		});

		ivCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}

		});
	}

	private void initData() {
		mList.add(new AttributeInfo("4.2", "4.2米"));
		mList.add(new AttributeInfo("4.8", "4.8米"));
		mList.add(new AttributeInfo("5.0", "5.0米"));
		mList.add(new AttributeInfo("5.2", "5.2米"));
		mList.add(new AttributeInfo("5.8", "5.8米"));
		mList.add(new AttributeInfo("6.2", "6.2米"));
		mList.add(new AttributeInfo("6.8", "6.8米"));
		mList.add(new AttributeInfo("7.2", "7.2米"));
		mList.add(new AttributeInfo("8.6", "8.6米"));

		mList.add(new AttributeInfo("9.6", "9.6米"));
		mList.add(new AttributeInfo("10", "10米"));
		mList.add(new AttributeInfo("12.5", "12.5米"));
		mList.add(new AttributeInfo("13", "13米"));
		mList.add(new AttributeInfo("16", "16米"));
		mList.add(new AttributeInfo("17.5", "17.5米"));
		mList.add(new AttributeInfo("23", "23米以上"));
	}

	class SimpleAdapter extends BaseAdapter {
		LayoutInflater inflater;
		ViewHold viewhold = null;

		public SimpleAdapter() {
			super();
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@SuppressLint("InflateParams")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			viewhold = null;
			if (convertView == null) {
				convertView = inflater.inflate(
						R.layout.view_screeing_gridview_item, null);
				viewhold = new ViewHold();
				viewhold.itemText = (TextView) convertView
						.findViewById(R.id.text);
				convertView.setTag(viewhold);
			} else {
				viewhold = (ViewHold) convertView.getTag();
			}
			try {
				viewhold.itemText.setText(mList.get(position).getName());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return convertView;
		}

		class ViewHold {
			TextView itemText;
		}
	}

	public interface OnSelectListener {
		public void selected(AttributeInfo info);
	}

}
